# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from random import randint

from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError, ValidationError
from odoo.osv import expression


class ProductAttributeExt(models.Model):
    _inherit = "product.attribute"
    _description = "Product Attribute"
    # if you change this _order, keep it in sync with the method
    # `_sort_key_attribute_value` in `product.template`

    limit_type = fields.Selection([
        ('param', 'Product Param'),
        ('spec', 'Product Specification'),
        ('route', 'Route Param'),
        ('pur', 'Purchase Attribute')], default='pur', required=True, help="The display type used in the Product Configurator.")
    param_line_ids = fields.One2many('product.template.param.line', 'attribute_id', 'Lines')
    #spec_line_ids = fields.One2many('product.template.attribute.line', 'attribute_id', 'Lines')
    #route_param_line_ids = fields.One2many('product.template.attribute.line', 'attribute_id', 'Lines')
    #pur_attribute_line_ids = fields.One2many('product.template.attribute.line', 'attribute_id', 'Lines')
    code = fields.Char(string="Code")

    thai_name = fields.Char(string="Thai Name")
    eng_name = fields.Char(string="English Name")

    @api.depends('name', 'code')
    def _compute_display_name(self):
        for rec in self:
            if rec.code and rec.name:
                rec.display_name = '[' + rec.code + ' ] ' + rec.name
            else:
                rec.display_name = rec.name

    _sql_constraints = [
        ('code_unique', 'UNIQUE(code)', 'The code must be unique!')
    ]

class ProductAttributeValue(models.Model):
    _inherit = "product.attribute.value"

    thai_name = fields.Char(string="Thai Name")
    eng_name = fields.Char(string="English Name")

class ProductTemplateParamLine(models.Model):
    """Attributes available on product.template with their selected values in a m2m.
    Used as a configuration model to generate the appropriate product.template.attribute.value"""
    _name = "product.template.param.line"
    _rec_name = 'attribute_id'
    _rec_names_search = ['attribute_id']
    _description = 'Product Template Param Line'
    _order = 'attribute_id, id'

    active = fields.Boolean(default=True)
    product_tmpl_id = fields.Many2one('product.template', string="Product Template", ondelete='cascade',
                                    required=True, index=True)
    attribute_id = fields.Many2one('product.attribute', string="Attribute", domain="[('limit_type', '=', 'param')]",
                                   ondelete='restrict', required=True, index=True)
    thai_name = fields.Char(string="Thai Name", )
    eng_name = fields.Char(string="English Name", )

    value_id = fields.Many2one('product.attribute.value', string="Values",
                               domain="[('attribute_id', '=', attribute_id)]")

    z_thai_name = fields.Char(string="Value T Name",  )
    z_eng_name = fields.Char(string="Value E Name", )

    @api.model_create_multi
    def create(self, vals_list):
        self.attribute_id=self.attribute_id.filtered(lambda pav: pav.limit_type == 'param')
        return super(ProductTemplateParamLine, self).create(vals_list)

    @api.constrains('attribute_id', 'value_id')
    @api.onchange('attribute_id', 'value_id')
    def _onchange_attribute_id(self):
        for record in self:
            if record.attribute_id:
                record.thai_name = record.attribute_id.thai_name
                record.eng_name = record.attribute_id.eng_name
            if record.value_id:
                record.z_thai_name = record.value_id.thai_name
                record.z_eng_name = record.value_id.eng_name

    # @api.constrains('active', 'value_ids', 'attribute_id')
    # def _check_valid_values(self):
    #     for ptal in self:
    #         if ptal.active and not ptal.value_ids:
    #             raise ValidationError(
    #                 _("The attribute %s must have at least one value for the product %s.") %
    #                 (ptal.attribute_id.display_name, ptal.product_tmpl_id.display_name)
    #             )
    #         for pav in ptal.value_ids:
    #             if pav.attribute_id != ptal.attribute_id:
    #                 raise ValidationError(
    #                     _("On the product %s you cannot associate the value %s with the attribute %s because they do not match.") %
    #                     (ptal.product_tmpl_id.display_name, pav.display_name, ptal.attribute_id.display_name)
    #                 )
    #     return True

class ProductTemplatePurParamLine(models.Model):
    """Attributes available on product.template with their selected values in a m2m.
    Used as a configuration model to generate the appropriate product.template.attribute.value"""
    _name = "product.template.pur.param.line"
    _rec_name = 'attribute_id'
    _rec_names_search = ['attribute_id']
    _description = 'Product Template Purchase Param Line'
    _order = 'attribute_id, id'

    active = fields.Boolean(default=True)
    product_tmpl_id = fields.Many2one('product.template', string="Product Template", ondelete='cascade',
                                    required=True, index=True)
    attribute_id = fields.Many2one('product.attribute', string="Attribute", domain="[('limit_type', '=', 'pur')]",
                                   ondelete='restrict', required=True, index=True)
    thai_name = fields.Char(string="Thai Name", )
    eng_name = fields.Char(string="English Name", )

    value_id = fields.Many2one('product.attribute.value', string="Values",
                               domain="[('attribute_id', '=', attribute_id)]")

    z_thai_name = fields.Char(string="Value T Name", )
    z_eng_name = fields.Char(string="Value E Name", )

    @api.model_create_multi
    def create(self, vals_list):
        self.attribute_id=self.attribute_id.filtered(lambda pav: pav.limit_type == 'pur')
        return super(ProductTemplatePurParamLine, self).create(vals_list)

    @api.constrains('attribute_id', 'value_id')
    @api.onchange('attribute_id', 'value_id')
    def _onchange_attribute_id(self):
        for record in self:
            if record.attribute_id:
                record.thai_name = record.attribute_id.thai_name
                record.eng_name = record.attribute_id.eng_name
            if record.value_id:
                record.z_thai_name = record.value_id.thai_name
                record.z_eng_name = record.value_id.eng_name

    # @api.constrains('active', 'value_ids', 'attribute_id')
    # def _check_valid_values(self):
    #     for ptal in self:
    #         if ptal.active and not ptal.value_ids:
    #             raise ValidationError(
    #                 _("The attribute %s must have at least one value for the product %s.") %
    #                 (ptal.attribute_id.display_name, ptal.product_tmpl_id.display_name)
    #             )
    #         for pav in ptal.value_ids:
    #             if pav.attribute_id != ptal.attribute_id:
    #                 raise ValidationError(
    #                     _("On the product %s you cannot associate the value %s with the attribute %s because they do not match.") %
    #                     (ptal.product_tmpl_id.display_name, pav.display_name, ptal.attribute_id.display_name)
    #                 )
    #     return True

class ProductTemplateSpecLine(models.Model):
    """Attributes available on product.template with their selected values in a m2m.
    Used as a configuration model to generate the appropriate product.template.attribute.value"""
    _name = "product.template.spec.line"
    _rec_name = 'attribute_id'
    _rec_names_search = ['attribute_id',]
    _description = 'Product Template Specification Line'
    _order = 'attribute_id, id'

    active = fields.Boolean(default=True)
    product_tmpl_id = fields.Many2one(
        'product.template', string="Product Template", ondelete='cascade', required=True, index=True)
    attribute_id = fields.Many2one(
        'product.attribute', string="Attribute", domain="[('limit_type', '=', 'spec')]", ondelete='restrict',
        required=True, index=True
    )
    thai_name = fields.Char(string="Thai Name", )
    eng_name = fields.Char(string="English Name", )

    value_id = fields.Many2one(
        'product.attribute.value', string="Values", domain="[('attribute_id', '=', attribute_id)]")

    z_thai_name = fields.Char(string="Value T Name", )
    z_eng_name = fields.Char(string="Value E Name", )

    @api.model_create_multi
    def create(self, vals_list):
        self.attribute_id=self.attribute_id.filtered(lambda pav: pav.limit_type == 'spec')
        return super(ProductTemplateSpecLine, self).create(vals_list)

    @api.constrains('attribute_id', 'value_id')
    @api.onchange('attribute_id', 'value_id')
    def _onchange_attribute_id(self):
        for record in self:
            if record.attribute_id:
                record.thai_name = record.attribute_id.thai_name
                record.eng_name = record.attribute_id.eng_name
            if record.value_id:
                record.z_thai_name = record.value_id.thai_name
                record.z_eng_name = record.value_id.eng_name

    # @api.constrains('active', 'value_ids', 'attribute_id')
    # def _check_valid_values(self):
    #     for ptal in self:
    #         if ptal.active and not ptal.value_ids:
    #             raise ValidationError(
    #                 _("The attribute %s must have at least one value for the product %s.") %
    #                 (ptal.attribute_id.display_name, ptal.product_tmpl_id.display_name)
    #             )
    #         for pav in ptal.value_ids:
    #             if pav.attribute_id != ptal.attribute_id:
    #                 raise ValidationError(
    #                     _("On the product %s you cannot associate the value %s with the attribute %s because they do not match.") %
    #                     (ptal.product_tmpl_id.display_name, pav.display_name, ptal.attribute_id.display_name)
    #                 )
    #     return True





